package com.baizhi.controller;


import com.baizhi.constants.RedisPrefix;
import com.baizhi.entity.User;
import com.baizhi.po.PlayedPO;
import com.baizhi.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 收藏(Favorite)表控制层
 *
 * @author makejava
 * @since 2021-09-28 11:00:09
 */
@RefreshScope
@RestController
@RequestMapping
public class FavoriteController {
    /**
     * 服务对象
     */
    @Resource
    private FavoriteService favoriteService;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/user/favorites")
    public List<PlayedPO> queryAll(@RequestParam("token") String token,
                                   @RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                   @RequestParam(value = "per_page", defaultValue = "1", required = false) Integer per_page) {
        //根据token，获取当前登录用户
        User user = (User) redisTemplate.opsForValue().get(RedisPrefix.USER_TOKEN_PREFIX + token);
        Integer userId = user.getId();
        List<PlayedPO> playedPOS = favoriteService.queryAll(userId, page, per_page);
        return playedPOS;
    }
}

